import {SkuCode} from "./sku-code";
import {CellStatus} from "../../core/enum";
import {SkuPending} from "./sku-pending";
import {Joiner} from "../../utils/joiner";

class Judger {
    fenceGroup
    pathDict = []
    skuPending

    constructor(fenceGroup) {
        this.fenceGroup = fenceGroup
        this._initPathDict()
        this._initSkuPending()
    }

    _initSkuPending() {
        this.skuPending = new SkuPending(this.fenceGroup.fences.length)
        const defaultSku = this.fenceGroup.getDefaultSku()
        if (!defaultSku) return
        // 默认规格
        this.skuPending.initDefaultSkuCell(defaultSku)
        // 渲染默认规格
        this.skuPending.pending.forEach(cell => {
            this.fenceGroup.setCellStatusById(cell.id, CellStatus.SELECTED)
        })
        this.judge(null, null, null, true)
    }

    // 所有可选规格的路径
    _initPathDict() {
        this.fenceGroup.spu.sku_list.forEach(sku => {
            const skuCode = new SkuCode(sku.code)
            this.pathDict.push(...skuCode.segments)
        })
    }

    judge(cell, x, y, init = false) {
        if (!init) {
            this._changeCurrentCheckedCellStatus(cell, x, y)
        }
        // 遍历所有cell,找出所有的可选路径
        this.fenceGroup.eachCell((cell, x, y) => {
            const path = this._findPotentialPath(cell, x, y)
            if (!path) return  // 对应56行
            // 判断当前cell生成的可选路径是否在字典中, 一个cell只会对应一个可选路径
            const isInDict = this.pathDict.includes(path)
            if (isInDict) {
                this.fenceGroup.setCellStatusByXY(x, y, CellStatus.WAITING)
            } else {
                this.fenceGroup.setCellStatusByXY(x, y, CellStatus.FORBIDDEN)
            }
        })
    }

    isSkuIntact() {
        return this.skuPending.isIntact()
    }

    // 获取规格选择完毕的sku
    getDeterminateSku(){
        const code = this.skuPending.getSkuCode()
        return this.fenceGroup.getSku(code)
    }

    // 获取已选择的规格值
    getCurrentValues() {
        return this.skuPending.getCurrentSpecValues()
    }

    // 获取未选择的规格值
    getMissingKeys() {
        const missingKeysIndex = this.skuPending.getMissingSpecKeysIndex()
        return missingKeysIndex.map(i => this.fenceGroup.fences[i].title)
    }

    /**
     * 当前正在遍历的cell 与 其他行的已选元素 组成可选路径
     * @param cell 当前正在遍历的cell
     * @param x 行号
     * @param y
     * @returns {string}
     * @private
     */
    _findPotentialPath(cell, x, y) {
        const joiner = new Joiner('#')
        for (let i = 0; i < this.fenceGroup.fences.length; i++) {
            // 获取每一行的已选元素
            const selected = this.skuPending.findSelectedCellByX(i)
            // 当前行
            if (x === i) {
                if (this.skuPending.isSelected(cell, x)) return // 当前行,且被选中,跳过, 不会将当前cell改为WAITING状态
                const cellCode = this._getCellCode(cell.spec)
                joiner.join(cellCode)
            } else {
                // 其他行
                if (selected) {
                    const selectedCellCode = this._getCellCode(selected.spec)
                    joiner.join(selectedCellCode)
                }
            }
        }
        return joiner.getStr()
    }

    _getCellCode(spec) {
        return spec.key_id + '-' + spec.value_id
    }

    _changeCurrentCheckedCellStatus(cell, x, y) {
        if (cell.status === CellStatus.WAITING) {
            this.fenceGroup.setCellStatusByXY(x, y, CellStatus.SELECTED)
            this.skuPending.insertCell(cell, x)
        }
        if (cell.status === CellStatus.SELECTED) {
            this.fenceGroup.setCellStatusByXY(x, y, CellStatus.WAITING)
            this.skuPending.removeCell(x)
        }
    }
}

export {
    Judger
}
